package create

import (
	sreinfo "ServiceEdge/controllers/src/srcInfo"
	client "ServiceEdge/pkg/client/clientset"
	"context"
	"github.com/sirupsen/logrus"
	appsv1 "k8s.io/api/apps/v1"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
)

func createNamespace(clientset *kubernetes.Clientset, name string) error {
	namespace := &v1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: name,
		},
	}
	_, err := clientset.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{})
	return err
}

func createService(clientset *kubernetes.Clientset, namespace, serviceName, selectorKey, selectorValue string) error {
	service := &v1.Service{
		ObjectMeta: metav1.ObjectMeta{
			Name:      serviceName,
			Namespace: namespace,
		},
		Spec: v1.ServiceSpec{
			Selector: map[string]string{
				selectorKey: selectorValue,
			},
			Ports: []v1.ServicePort{
				{
					Port: 80,
				},
			},
		},
	}
	_, err := clientset.CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{})
	return err
}

func createDeployment(clientset *kubernetes.Clientset, namespace, deploymentName, containerName, image string) error {
	deployment := &appsv1.Deployment{
		ObjectMeta: metav1.ObjectMeta{
			Name:      deploymentName,
			Namespace: namespace,
		},
		Spec: appsv1.DeploymentSpec{
			Replicas: int32Ptr(1),
			Selector: &metav1.LabelSelector{
				MatchLabels: map[string]string{"app": deploymentName},
			},
			Template: v1.PodTemplateSpec{
				ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": deploymentName}},
				Spec: v1.PodSpec{
					Containers: []v1.Container{
						{
							Name:  containerName,
							Image: image,
						},
					},
				},
			},
		},
	}
	_, err := clientset.AppsV1().Deployments(namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
	return err
}

func createPod(clientset *kubernetes.Clientset, namespace, podName, containerName, image string) error {
	pod := &v1.Pod{
		ObjectMeta: metav1.ObjectMeta{
			Name:      podName,
			Namespace: namespace,
		},
		Spec: v1.PodSpec{
			Containers: []v1.Container{
				{
					Name:  containerName,
					Image: image,
				},
			},
		},
	}
	_, err := clientset.CoreV1().Pods(namespace).Create(context.TODO(), pod, metav1.CreateOptions{})
	return err
}
func createPodWithPod(clientset *kubernetes.Clientset, pod *v1.Pod) error {
	_, err := clientset.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, metav1.CreateOptions{})
	return err
}

// Helper function to convert int32 to pointer
func int32Ptr(i int32) *int32 {
	return &i
}

func CteatePodByNodeLable(podInfo *sreinfo.PodInfo, nodeSelectorInfo *sreinfo.NodeSelectorInfo) error {
	client := client.DefaultClient()
	nodeSelector := nodeSelectorInfo.CreateNodeSelector()
	// 创建 Pod 对象
	pod := &v1.Pod{
		ObjectMeta: metav1.ObjectMeta{
			Name:      podInfo.Name,
			Namespace: podInfo.Namespace,
		},
		Spec: v1.PodSpec{
			Containers: []v1.Container{
				{
					Name:  podInfo.Name + "-image",
					Image: podInfo.Image,
				},
			},
			NodeSelector: nodeSelector,
		},
	}
	err := createPodWithPod(client, pod)
	if err != nil {
		logrus.Error(err)
		return err
	}
	return nil
}
